package com.ruoyi.basedata.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.basedata.mapper.BaseReportMapper;
import com.ruoyi.basedata.service.IBaseReportService;
import com.ruoyi.basedata.util.CustomExcelUtil;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;

/**
 * 基础数据报表服务实现
 * 
 * @author ruoyi
 */
@Service
public class BaseReportServiceImpl implements IBaseReportService
{
    private static final Logger log = LoggerFactory.getLogger(BaseReportServiceImpl.class);
    
    @Autowired
    private BaseReportMapper baseReportMapper;

    /**
     * 查询商品名称和单价统计数据
     * 
     * @return 商品名称和单价统计数据列表
     */
    @Override
    public List<Map<String, Object>> selectProductCountData()
    {
        try {
            List<Map<String, Object>> data = baseReportMapper.selectProductCountData();
            return data != null ? data : new ArrayList<>();
        } catch (Exception e) {
            log.error("查询商品统计数据出错", e);
            return new ArrayList<>();
        }
    }
    
    /**
     * 查询商品采购量和销售量统计数据
     * 
     * @return 商品采购量和销售量统计数据列表
     */
    @Override
    public List<Map<String, Object>> selectProductVolumeData()
    {
        try {
            List<Map<String, Object>> data = baseReportMapper.selectProductVolumeData();
            return data != null ? data : new ArrayList<>();
        } catch (Exception e) {
            log.error("查询商品采购量和销售量数据出错", e);
            return new ArrayList<>();
        }
    }
    
    /**
     * 导出商品单价统计数据
     * 
     * @param params 查询参数
     * @return 导出的文件名
     */
    @Override
    public String exportProductPriceData(Map<String, Object> params)
    {
        List<Map<String, Object>> data = selectProductCountData();
        
        // 构建导出数据
        List<ProductPriceExport> exportList = new ArrayList<>();
        for (Map<String, Object> item : data) {
            ProductPriceExport export = new ProductPriceExport();
            export.setProductName(Convert.toStr(item.get("name"), ""));
            export.setPrice(Convert.toDouble(item.get("value"), 0.0));
            exportList.add(export);
        }
        
        // 生成文件名
        String fileName = "商品单价统计_" + DateUtils.dateTimeNow() + ".xlsx";
        
        // 导出Excel
        CustomExcelUtil<ProductPriceExport> util = new CustomExcelUtil<>(ProductPriceExport.class);
        return util.exportExcelToFile(exportList, "商品单价统计", fileName);
    }
    
    /**
     * 导出商品采购量和销售量统计数据
     * 
     * @param params 查询参数
     * @return 导出的文件名
     */
    @Override
    public String exportProductVolumeData(Map<String, Object> params)
    {
        List<Map<String, Object>> data = selectProductVolumeData();
        
        // 构建导出数据
        List<ProductVolumeExport> exportList = new ArrayList<>();
        for (Map<String, Object> item : data) {
            ProductVolumeExport export = new ProductVolumeExport();
            export.setProductName(Convert.toStr(item.get("name"), ""));
            export.setPurchaseVolume(Convert.toInt(item.get("purchase"), 0));
            export.setSalesVolume(Convert.toInt(item.get("sales"), 0));
            exportList.add(export);
        }
        
        // 生成文件名
        String fileName = "商品采购销售量统计_" + DateUtils.dateTimeNow() + ".xlsx";
        
        // 导出Excel
        CustomExcelUtil<ProductVolumeExport> util = new CustomExcelUtil<>(ProductVolumeExport.class);
        return util.exportExcelToFile(exportList, "商品采购销售量统计", fileName);
    }
    
    /**
     * 商品单价导出对象
     */
    public static class ProductPriceExport {
        /** 商品名称 */
        @Excel(name = "商品名称")
        private String productName;
        
        /** 单价 */
        @Excel(name = "单价(元)")
        private Double price;
        
        public String getProductName() {
            return productName;
        }
        
        public void setProductName(String productName) {
            this.productName = productName;
        }
        
        public Double getPrice() {
            return price;
        }
        
        public void setPrice(Double price) {
            this.price = price;
        }
    }
    
    /**
     * 商品采购销售量导出对象
     */
    public static class ProductVolumeExport {
        /** 商品名称 */
        @Excel(name = "商品名称")
        private String productName;
        
        /** 采购量 */
        @Excel(name = "采购量(件)")
        private Integer purchaseVolume;
        
        /** 销售量 */
        @Excel(name = "销售量(件)")
        private Integer salesVolume;
        
        public String getProductName() {
            return productName;
        }
        
        public void setProductName(String productName) {
            this.productName = productName;
        }
        
        public Integer getPurchaseVolume() {
            return purchaseVolume;
        }
        
        public void setPurchaseVolume(Integer purchaseVolume) {
            this.purchaseVolume = purchaseVolume;
        }
        
        public Integer getSalesVolume() {
            return salesVolume;
        }
        
        public void setSalesVolume(Integer salesVolume) {
            this.salesVolume = salesVolume;
        }
    }
} 